import Mock from 'mockjs' // 引入mockjs
import cityDataOrg from './city.json'

function getQueryString (hrefStr) {
  const arr = hrefStr.split('?')[1].split('&')
  let obj = {}
  for (let i of arr) {
    obj[i.split('=')[0]] = i.split('=')[1]
  }
  return obj
}

function getHotCity () {
  let num = Mock.Random.natural(0, 2)
  let letter = Mock.mock(/[A-Z]/)
  if (cityDataOrg.cities[letter]) {
    return cityDataOrg.cities[letter][num]
  } else {
    return null
  }
}

const getData = () => {
  const HomeIconsData = Mock.mock({
    'iconList|8-15': [
      {
        'id|+1': 0,
        url: Mock.Random.image('200x200')
      }
    ]
  })
  const HomeRecommondData = Mock.mock({
    'recommendList|2-5': [
      {
        'id|+1': 0,
        url: Mock.Random.image('200x200'),
        theme: '@ctitle(15,20)',
        desc: '@cparagraph',
        'price|1-1400': 1
      }
    ]
  })
  const HomeSwiperData = Mock.mock({
    'swiperList|2-5': [
      {
        'id|+1': 0,
        url: Mock.Random.image('414x207')
      }
    ]
  })
  const HomeWeekendData = Mock.mock({
    'weekendList|1-5': [
      {
        'id|+1': 0,
        url: Mock.Random.image('414x165'),
        theme: '@ctitle(15,20)',
        desc: '@cparagraph'
      }
    ]
  })
  const HomeHeaderData = Mock.mock({
    city: '@city'
  })
  return {
    HomeIconsData,
    HomeRecommondData,
    HomeSwiperData,
    HomeWeekendData,
    HomeHeaderData
  }
}

const getCityData = () => {
  const CityListData = {
    hotCities: []
  }
  for (let i = 0; i < Mock.Random.natural(8, 14); i++) {
    const city = getHotCity()
    if (city) {
      CityListData.hotCities.push(city)
    }
  }
  // 去重
  CityListData.hotCities = [...new Set(CityListData.hotCities)]
  const CityAlphabetData = {
    alphabetList: cityDataOrg.cities
  }
  return {
    CityListData,
    CityAlphabetData
  }
}

const getDetail = id => {
  const DetailBannerData = Mock.mock({
    sightName: `bh${id}_@ctitle(5,15)`,
    bannerImg: Mock.Random.image('375x206.25'),
    'gallaryImgList|5-10': [Mock.Random.image('375x375')]
  })

  const DetailListData = Mock.mock({
    'detailList|10-15': [
      {
        id: '@id',
        title: `0-bh-${id}_@ctitle(5,15)`,
        'children|0-2': [
          {
            id: '@id',
            title: `1-${id}_@ctitle(5,15)`,
            'children|0-1': [
              {
                id: '@id',
                title: `2-${id}_@ctitle(5,15)`
              }
            ]
          }
        ]
      }
    ]
  })
  return { DetailBannerData, DetailListData }
}

Mock.mock(/(\/api\/)([a-z]{2,4})(\/proxy\/query_common_home)/, 'post', () => {
  return {
    ret: 0,
    data: getData()
  }
}) // 根据数据模板生成模拟数据

Mock.mock(/(\/api\/)([a-z]{2,4})(\/proxy\/query_common_city)/, 'post', () => {
  return {
    ret: 0,
    status: 200,
    data: getCityData()
  }
}) // 根据数据模板生成模拟数据

Mock.mock(/(\/api\/)([a-z]{2,4})(\/proxy\/query_detail)/, 'post', options => {
  const id = getQueryString(options.url).id || ''
  return {
    ret: 0,
    status: 200,
    data: getDetail(id)
  }
}) // 根据数据模板生成模拟数据

Mock.setup({
  timeout: '200-600'
})
